/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.unavarra.mti.iws.utils;

import es.unavarra.mti.iws.logic.properties.KProperty;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Asier
 */
public class ReflectionUtils {

    public static String toStringNull(Object o){
        if(o == null){
            return "null";
        }else{
            return o.toString();
        }
    }
    
    public static boolean isSimpleType(Class t){
        if      (t == Byte.TYPE || t == Byte.class) { return true; }
        else if (t == Short.TYPE || t == Short.class) { return true; }
        else if (t == Integer.TYPE || t == Integer.class) { return true; }
        else if (t == Long.TYPE || t == Long.class) { return true; }
        else if (t == Float.TYPE || t == Float.class) { return true; }
        else if (t == Double.TYPE || t == Double.class) { return true; }
        else if (t == Boolean.TYPE || t == Boolean.class) { return true; }
        else if (t == Character.TYPE || t == Character.class) { return true; }
        else if (t == String.class) { return true;}
        else if (t.isEnum() ) { return true;}

        return false;
    }

    public static Object stringToSimpleObject(Class t, String value){
        if      (t == Byte.TYPE || t == Byte.class) { return Byte.parseByte(value); }
        else if (t == Short.TYPE || t == Short.class) { return Short.parseShort(value); }
        else if (t == Integer.TYPE || t == Integer.class) { return Integer.parseInt(value); }
        else if (t == Long.TYPE || t == Long.class) { return Long.parseLong(value); }
        else if (t == Float.TYPE || t == Float.class) { return Float.parseFloat(value); }
        else if (t == Double.TYPE || t == Double.class) { return Double.parseDouble(value); }
        else if (t == Boolean.TYPE || t == Boolean.class) { return Boolean.parseBoolean(value); }
        else if (t == Character.TYPE || t == Character.class) { return value.charAt(0); }
        else if (t == String.class) { return value; }
        else if (t.isEnum() ) {
            return Enum.valueOf(t, value);
        }

        return value;
    }
    
    public static KProperty createProperty(Class c, String name){
        String pascalCase = Character.toUpperCase( name.charAt(0) ) + name.substring(1);
        String getName = "get" + pascalCase;
        String isName = "is" + pascalCase;
        String setName = "set" + pascalCase;

        Method get = getMethod(c, getName, 0);
        Method set = getMethod(c, setName, 1);
        Method is = getMethod(c, isName, 0);

        Class returnType = get!=null? get.getReturnType() : is.getReturnType();

        return new KProperty(name, true, get!=null?get:is, set, returnType);
    }

    public static Field getField(Class c, String name){
        try {
            return c.getField(name);
        } catch (NoSuchFieldException ex) {
            Logger.getLogger(ReflectionUtils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(ReflectionUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static Method getMethod(Class c, String name,int withParameters){
        Method[] methods = c.getMethods();
        for(Method m : methods){
            if(m.getName().equals(name) && m.getParameterTypes().length == withParameters){
                return m;
            }
        }
        return null;
    }
}
